//
//  YCPickerContainerView.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/7.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCPickerContainerView.h"

@interface YCPickerContainerView ()
{
    //主视图
    UIView *_mainView;
    
    //工具栏
    UIToolbar *_toolbar;
    
    CGRect _mainViewShowFrame;
    
    CGRect _mainViewHideFrame;
    
    //标题视图
    UIBarButtonItem *_titleItem;
}

//主视图
- (UIView *)mainView;

//标题视图
- (UIBarButtonItem *)titleItem;

@end

@implementation YCPickerContainerView

//委托
@synthesize delegate = _delegate;
//取消按钮和确认按钮
@synthesize cancelButtonItem = _cancelButtonItem;
//确定按钮
@synthesize doneButtonItem   = _doneButtonItem;

@synthesize pickerView = _pickerView;

- (id)initWithFrame:(CGRect)frame
{
    return self = [super initWithFrame:[UIScreen mainScreen].bounds];
}

//设置标题
- (void)setTitle:(NSString *)title
{
    self.titleItem.title = title;
}

//获取标题
- (NSString *)title
{
    return _titleItem.title;
}

//返回可见视图的尺寸
- (CGRect)visibleFrame
{
    return _mainViewShowFrame;
}

//返回主视图
- (UIView *)mainView
{
    if(_mainView == nil)
    {
        CGRect rect = self.bounds;
        rect.origin.y = rect.size.height;
        
        //隐藏状态时尺寸
        _mainViewHideFrame = rect;
        
        rect.size.height = 260;
        rect.origin.y -= rect.size.height;
        //显示状态尺寸
        _mainViewShowFrame = rect;
        
        _mainView = [[UIView alloc]initWithFrame:_mainViewHideFrame];
        //自动适应宽度和顶部尺寸
        _mainView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        // 改变主视图的背景色
        self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5];
        [self addSubview:_mainView];
    }
    
    return _mainView;
}

//标题视图
- (UIBarButtonItem *)titleItem
{
    if(!_titleItem)
    {
        _titleItem = [[UIBarButtonItem alloc]init];
    }
    return _titleItem;
}

- (UIBarButtonItem *)cancelButtonItem
{
    if(_cancelButtonItem == nil)
    {
        _cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"    取消", nil) style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemHandler:)];
        [_cancelButtonItem setBackgroundImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(40, 43)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [_cancelButtonItem setBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];
    }
    return _cancelButtonItem;
}

- (UIBarButtonItem *)doneButtonItem
{
    if(_doneButtonItem == nil)
    {
        _doneButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"确定    ", nil)
                                                           style:UIBarButtonItemStylePlain
                                                          target:self
                                                          action:@selector(buttonItemHandler:)];
        _doneButtonItem.tintColor = [UIColor yc_colorWithUInt:0x00b4ff];
        
        [_doneButtonItem setBackgroundImage:[UIImage yc_imageWithColor:[UIColor clearColor] size:CGSizeMake(40, 43)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [_doneButtonItem setBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];
    }
    return _doneButtonItem;
}

//工具栏
- (UIToolbar *)toolbar
{
    if(!_toolbar)
    {
        @autoreleasepool
        {
            CGRect rect = self.mainView.bounds;
            rect.size.height = 44;
            //初始化
            _toolbar = [[UIToolbar alloc]initWithFrame:rect];
            _toolbar.tintColor = [UINavigationBar appearance].tintColor;
            
            if([_toolbar respondsToSelector:@selector(barTintColor)])
            {
                CGRect rect = _toolbar.bounds;
                rect.origin.y = CGRectGetMaxY(rect) - 1.0f;
                rect.size.height = 0.5f;
                
                //                UIView *line = [[UIView alloc]initWithFrame:rect];
                //                line.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
                //                line.backgroundColor = [UIColor yc_colorWithUInt:0xb2b2b2];
                //
                //                [_toolbar addSubview:line];
            }
            else
            {
                //标题
                _toolbar.barStyle = UIBarStyleBlackTranslucent;
            }
            
            _toolbar.items = @[
                               self.cancelButtonItem,
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               self.titleItem,
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               self.doneButtonItem];
            
            [self.mainView addSubview:_toolbar];
        }
    }
    return _toolbar;
}


- (void)buttonItemHandler:(UIBarButtonItem *)sender
{
    if(sender == self.doneButtonItem)
    {
        if([self.delegate respondsToSelector:@selector(pickerContainerViewDidDone:)])
        {
            [self.delegate pickerContainerViewDidDone:self];
        }
        
        if([self.delegate respondsToSelector:@selector(pickerContainerViewShouldEntryNext:)])
        {
            if([self.delegate pickerContainerViewShouldEntryNext:self])
            {
                return;
            }
        }
    }
    else
    {
        if([self.delegate respondsToSelector:@selector(pickerContainerViewDidCancel:)])
        {
            [self.delegate pickerContainerViewDidCancel:self];
        }
    }
    [self hideAnimationWillStart];
}


//动画开始
- (void)hideAnimationWillStart
{
    if([self.delegate respondsToSelector:@selector(pickerContainerViewWillHide:)])
    {
        [self.delegate pickerContainerViewWillHide:self];
    }
    
    [UIView animateWithDuration:.25f
                          delay:0
                        options:(7 << 16)
                     animations:^{
                         _mainView.frame = _mainViewHideFrame;
                     }
                     completion:^(BOOL finished){
                         [_pickerView removeFromSuperview];
                         _pickerView = nil;
                         [self removeFromSuperview];
                     }];
    
}

//显示动态
- (void)showAnimationWillStart
{
    if([self.delegate respondsToSelector:@selector(pickerContainerViewWillShow:)])
    {
        [self.delegate pickerContainerViewWillShow:self];
    }
    
    [UIView animateWithDuration:.25f
                          delay:0
                        options:(7 << 16)
                     animations:^{
                         self.mainView.frame = _mainViewShowFrame;
                     }
                     completion:NULL];
}

//点击任意位置关闭
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:_mainView];
    
    if(![_mainView pointInside:point withEvent:event]){
        [self hideAnimationWillStart];
    }
}

//弹出
- (void)showView:(UIView *)subview inView:(UIView *)view{
    //添加到父视图
    [self.mainView addSubview:subview];
    
    CGRect rect = _mainViewShowFrame;
    rect.origin.y  = self.toolbar.bounds.size.height;
    rect.size.height -= rect.origin.y;
    
    subview.frame = rect;
    
    //保存选择器
    _pickerView = subview;
    
    if(view.window != nil){
        view = view.window;
    }
    
    [view addSubview:self];
    
    //执行动态显示
    [self showAnimationWillStart];
}

//添加日期选择框
- (void)showDatePicker:(UIDatePicker *)datePicker inView:(UIView *)view{
    [self showView:datePicker inView:view];
}

//添加选择框
- (void)showPickerView:(UIPickerView *)pickerView inView:(UIView *)view{
    [self showView:pickerView inView:view];
}

@end
